package cn.edu.ecnu.gis.client.view;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

public class GoogleMap extends HTML {

    private static int nextId = 0;
    private JavaScriptObject map;

    public GoogleMap(Panel panel) {
        String id = "map" + (++nextId);
        setStyleName("gwtapps-GoogleMap");
        setHTML("<div id='" + id + "' style='height:300'></div>");
        RootPanel.get().add(this);
        //panel.add(this);
        map = initMap(id);
    }

    public void addMarker(JSONArray point) {
        addMarker(map, point);

    }
//[instance-expr.]@class-name::method-name(param-signature)(arguments)
    private native static void addMarker(JavaScriptObject map, JSONArray point)/*-{
    //map.clearOverLays();
    var lat = point.@com.google.gwt.json.client.JSONArray::get(I)(0);
    var lon=point.@com.google.gwt.json.client.JSONArray::get(I)(1);
    var point1=new $wnd.GLatLng(lat/100000,lon/100000);
    //var point1=new $wnd.LTPoint(lat/100000,lon/100000);
    map.setCenter(point1,13);
    map.addOverlay(new $wnd.GMarker(point1));
    var mapControl = new $wnd.GMapTypeControl();
    var mapSmallContorl =new $wnd.GSmallMapControl();
    map.addControl(mapControl);
    map.addControl(mapSmallContorl);
    map.openInfoWindow(map.getCenter(),$document.createTextNode(lat, lon));
    }-*/;

    public void setCenter(final double lat, final double lon) {
        setCenterImpl(map, lat, lon);
    }

    private native static JavaScriptObject initMap(String id)/*-{
    var map = new $wnd.GMap2( $doc.getElementById(id) );
    return map;
    }-*/;

    private native static void setCenterImpl(JavaScriptObject map, double lat, double lon)/*-{
    map.setCenter(new $wnd.GLatLng(lat, lon), 13);
    }-*/;
}
